package com.atguigu.eduservice.service.impl;

import com.atguigu.common.exception.GuliException;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.EduCourseDescription;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.entity.coursevo.CourseInfoForm;
import com.atguigu.eduservice.entity.coursevo.CoursePublishVo;
import com.atguigu.eduservice.entity.coursevo.FrontCourseInfoVo;
import com.atguigu.eduservice.entity.coursevo.FrontCourseVo;
import com.atguigu.eduservice.mapper.EduCourseMapper;
import com.atguigu.eduservice.service.EduChapterService;
import com.atguigu.eduservice.service.EduCourseDescriptionService;
import com.atguigu.eduservice.service.EduCourseService;
import com.atguigu.eduservice.service.EduVideoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-04-13
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    //注入描述service
    @Autowired
    private EduCourseDescriptionService eduCourseDescriptionService;

    @Autowired
    private EduVideoService videoService;

    @Autowired
    private EduChapterService chapterService;

    //添加课程基本信息
    @Override
    public String addCourseInfo(CourseInfoForm courseInfoForm) {
        //1 添加到课程基本信息表
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoForm,eduCourse);
        //返回值：添加之后影响行数

        int insert = baseMapper.insert(eduCourse);
        if(insert == 0) { //添加失败
            throw new GuliException(20001,"添加课程信息失败");
        }

        //课程id
        String courseId = eduCourse.getId();

        //2 添加到课程描述表
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        //把添加之后课程id设置到描述对象里面，因为课程和描述是一对一
        eduCourseDescription.setId(courseId);
        eduCourseDescription.setDescription(courseInfoForm.getDescription());
        eduCourseDescriptionService.save(eduCourseDescription);

        return courseId;
    }

    //根据课程id查询课程信息
    @Override
    public CourseInfoForm getCourseInfo(String id) {
       //1 根据课程id查询课程基本信息
        EduCourse eduCourse = baseMapper.selectById(id);
        if(eduCourse == null) {
            throw new GuliException(20001,"数据不存在");
        }
        //eduCourse对象值复制到CourseInfoForm里面
        CourseInfoForm courseInfoForm = new CourseInfoForm();
        BeanUtils.copyProperties(eduCourse,courseInfoForm);

        //2根据课程id查询描述信息
        EduCourseDescription eduCourseDescription = eduCourseDescriptionService.getById(id);
        if(eduCourseDescription != null) {
            courseInfoForm.setDescription(eduCourseDescription.getDescription());
        }
        return courseInfoForm;
    }

    //修改课程信息
    @Override
    public void updateCourse(CourseInfoForm courseInfoForm) {
        //1 修改课程基本信息
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoForm,eduCourse);
        int result = baseMapper.updateById(eduCourse);
        if(result == 0) {
            throw new GuliException(20001,"修改失败");
        }

        //2 修改课程描述
        EduCourseDescription description = new EduCourseDescription();
        BeanUtils.copyProperties(courseInfoForm,description);
        eduCourseDescriptionService.updateById(description);
    }

    //根据课程id查询课程确认信息
    @Override
    public CoursePublishVo getPublishCourse(String id) {
        CoursePublishVo coursePublishVo = baseMapper.getPublishInfoCourse(id);
        return coursePublishVo;
    }

    //删除课程接口
    @Override
    public void deleteCourse(String courseId) {
        //1 根据课程id删除小节
        videoService.removeVideoByCourseId(courseId);

        //2 根据课程id删除章节
        chapterService.removeChapterByCourseId(courseId);

        //3 根据课程id删除描述
        eduCourseDescriptionService.removeById(courseId);

        //4 根据课程id删除课程
        int result = baseMapper.deleteById(courseId);
        if(result == 0) {
            throw new GuliException(20001,"删除课程失败");
        }
    }

    //1 条件查询分页课程列表
    @Override
    public Map<String, Object> getCourseFrontList(Page<EduCourse> pageParam, FrontCourseVo frontCourseVo) {
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();

        //查询已经发布课程
        wrapper.eq("status","Normal");

        //一级分类id
        if(!StringUtils.isEmpty(frontCourseVo.getSubjectParentId())) {
            wrapper.eq("subject_parent_id",frontCourseVo.getSubjectParentId());
        }
        //二级分类id
        if (!StringUtils.isEmpty(frontCourseVo.getSubjectId())) {
            wrapper.eq("subject_id", frontCourseVo.getSubjectId());
        }
        //销量
        if (!StringUtils.isEmpty(frontCourseVo.getBuyCountSort())) {
            wrapper.orderByDesc("buy_count");
        }
        //最新
        if (!StringUtils.isEmpty(frontCourseVo.getGmtCreateSort())) {
            wrapper.orderByDesc("gmt_create");
        }
        //价格
        if (!StringUtils.isEmpty(frontCourseVo.getPriceSort())) {
            wrapper.orderByDesc("price");
        }
        //调用方法
        baseMapper.selectPage(pageParam,wrapper);

        List<EduCourse> records = pageParam.getRecords();
        long current = pageParam.getCurrent();
        long pages = pageParam.getPages();
        long size = pageParam.getSize();
        long total = pageParam.getTotal();
        boolean hasNext = pageParam.hasNext();
        boolean hasPrevious = pageParam.hasPrevious();

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("items", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);

        return map;
    }

    //根据课程id查询课程基本信息，用于详情显示
    @Override
    public FrontCourseInfoVo getCourseBaseInfoFront(String courseId) {
        return baseMapper.getFrontCourseBaseInfo(courseId);
    }

}
